package com.syni.mdd.sjb.common.datatreaure.controller.protect.pay;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.pay.PayRqVo;
import com.syni.mdd.sjb.common.datatreaure.service.AlipayService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
@RequestMapping("/aliPay")
@Log4j2
public class AliPayController {

    @Autowired
    AlipayService alipayService;

    @PostMapping("prepay")
    @ApiOperation(value = "支付宝统一下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单ID"),
            @ApiImplicitParam(name = "orderNo",value = "订单NO")
    })
    @CheckUserIdentity
    Map<String,Object> prepay(@RequestBody @Validated PayRqVo payRqVo){
        return alipayService.prepay(payRqVo);
    }

    /**
     * 支付结果回调
     */
    @PostMapping("/AppAliPayNotify")
    public synchronized void appAliPayNotify() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try{
            alipayService.appAliPayNotify(request);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @PostMapping("/test")
    public synchronized void app(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        System.out.println("----------------阿里服务器消费手机回调-------" + request);
        Map<String, String> params = new HashMap<>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
            params.put(name, valueStr);
        }
        log.info("回调数据：" + JSON.toJSONString(params));
    }
}
